package com.example.demo;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * @author fanhb on 2020/7/27
 */
@Service
public class HelloHandler {

    RedisTemplate redisTemplate;
    public Mono<ServerResponse> hello(ServerRequest request) {

//        redisTemplate.get
        Mono<User> requestMono = request.bodyToMono(User.class);
        Mono<User> mapped = requestMono.map(user -> user)
                .doOnSuccess(s -> System.out.println(s));
        return ServerResponse.ok().body(mapped, User.class);
    }

    static class User {
        private String id;
        private String name;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return "User{" +
                    "id='" + id + '\'' +
                    ", name='" + name + '\'' +
                    '}';
        }
    }

    public static void main(String[] args) {


//        //Flux->Flux
//        Flux.from(Flux.just("just", "just1", "just2"))
//                .subscribe(System.out::println);
//        //Mono->Mono
//        Flux.from(Mono.just("just")).subscribe(System.out::println);

        Flux.defer(() -> Flux.just("just", "just1", "just2"))
                .subscribe(System.out::println);
    }
}
